<?php

namespace app\astro\controller;

use \app\check\controller\Check;
use app\common\model\Recode;

class Firdaria extends Check
{
    /**
     * @return \think\response\Json
     * @throws \app\common\lib\exception\ApiException
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function get()
    {
        return show(config('code.error'), '请更新后使用该功能！');
        $datetime = htmlspecialchars(input('datetime/s',''));

        $time = strtotime($datetime);
        $timeinfo = [
            'year' => date('Y', $time),
            'month' => date('m', $time),
            'day' => date('d', $time),
            'hour' => date('H', $time),
            'minute' => date('i', $time),
        ];
        $timeinfo['timezone'] = (int)(input('timezone'));
        $timeinfo['is_summer'] = isSummerTime($timeinfo['hour'], $timeinfo['minute'], 0,
            $timeinfo['month'], $timeinfo['day'], $timeinfo['year']) ? 1 : 0;

        $Data = new Data();

        $map = ['id' => input('id/d', 0), 'uid' => $this->user['id']];

        $RecodeModel = new Recode();

        $userinfo = $RecodeModel->getRecode($map);

        $timestamp1 = $Data->deal_time($userinfo);

        $utdatenow = strftime("%d.%m.%Y", $timestamp1);
        $utnow = strftime("%H:%M:%S", $timestamp1);

        $longitude = ($userinfo['long_deg'] + $userinfo['long_min'] / 60) * $userinfo['ew'];
        $latitude = ($userinfo['lat_deg'] + $userinfo['lat_min'] / 60) * $userinfo['ns'];

        $timestamp2 = $Data->deal_time($timeinfo);

//        $birth_JD = gregoriantojd(date('m',$timestamp1), date('d',$timestamp1), date('Y',$timestamp1)) + ((date('H',$timestamp1) + (date('i',$timestamp1) / 60)) / 24);
//        $start_JD = gregoriantojd(date('m',$timestamp2), date('d',$timestamp2), date('Y',$timestamp2)) + ((date('H',$timestamp2) + (date('i',$timestamp2) / 60)) / 24);
//        $days_alive = $start_JD - $birth_JD; // 总天数
        $days_alive = ($timestamp2 - $timestamp1) / 3600 / 24; // 总天数
        $years_alive = $days_alive / 365.2422;// 365.2422  365.2421990  365.25


        $Set = new \app\user\controller\Astroset();
        $astro_set = $Set->get(true, 'firdaria');// 内部调用
        $Data->planets_display = $astro_set['value']['planets_display'];
        $Data->asps_display = $astro_set['value']['asps_display'];
        $Data->asp_orb_all = $astro_set['value']['asp_orb_all'];
        $Data->planet_orb_all = $astro_set['value']['planet_orb_all'];

        $options = ['utdatenow' => $utdatenow, 'utnow' => $utnow, 'longitude' => $longitude, 'latitude' => $latitude, 'h_sys' => $astro_set['value']['h_sys']];

        $out = $Data->execData($options);

        $original_data = $Data->getOriginalData($out, $astro_set['value']['h_sys']);
        $Data->array_remove($original_data, $Data->planets_display);
        $details = $Data->getPlanetHouseDetail($original_data, $original_data['hc1']);
        $phase = $Data->planetPkPlanet($original_data, $original_data, true);
        foreach ($phase['planets'] as $k => $d) {
            $details['planets'][$k]['phase'] = $d['phase'];
            if (empty($d['phase'])) $details['planets'][$k]['phase'] = null;
        }
        $details['phase'] = $phase['phase'];

        // 大运、小运
        $main_sub_luck = $this->getTheMainSubLuck($timestamp1, $years_alive, $Data->day_chart, $astro_set['value']['firdaria']);
        $details['firdaria'] = $main_sub_luck;

        $details['planets'] = (object)$details['planets'];// 指定输出为object  防止所有行星全开时 类型改变

        return json(['code' => 200, 'data' => $details, 'msg' => 'Success.']);
    }

    /**
     * 大限、主限运势星
     */
    private function getTheMainSubLuck($birthtime = null, $years_alive = 0, $day_chart = false, $set = 'default')
    {
        // 日间盘
        if ($day_chart == true) $firdaria_main = config('astrolog.firdaria_main_day');
        // 夜间盘
        else $firdaria_main = config('astrolog.firdaria_main_night_' . $set);
        $years_alive_or = $years_alive;
        // 大于75岁 从头开始
        $years_alive = $years_alive - floor($years_alive / 75) * 75;
        $years = $years_temp = 0;
        $time_main = $time_sub = $birthtime;
        $main_luck = $sub_luck = null;

        $ret = [];
        foreach ($firdaria_main as $i => $f) {
            $y = config('astrolog.firdaria_planet_years')[$f];
            // 以当前大限主管星为起始星(小限)
            $sub_planets = $this->getTheSubPlanets($f);
            $pro_add_sub = count($sub_planets) == 1 ? $y : $y / 7;//大限平分七份为小限时间  南北交时为全部
            $pro_add_sub_time = $pro_add_sub * 365.2422 * 24 * 3600;

            $sub_details = [];
            foreach ($sub_planets as $j => $s) {
                $sub_details[] = [
                    'id' => $s,
                    'cross' => $pro_add_sub,
                    'year_min' => $years_temp,
                    'year_max' => $years_temp + $pro_add_sub,
                    'start' => date("y年m月d日", $time_sub),
                    'end' => date("y年m月d日", $time_sub + $pro_add_sub_time),
                ];
                $years_temp += $pro_add_sub;
                $time_sub += $pro_add_sub_time;

                if ($years_alive <= $years_temp && $sub_luck === null) $sub_luck = $s;
            }
            $ret['details'][] = [
                'main' => [
                    'id' => $f,
                    'cross' => $y,
                    'year_min' => $years,
                    'year_max' => $years + $y,
                    'start' => date("y年m月d日", $time_main),
                    'end' => date("y年m月d日", strtotime("+$y year", $time_main)),
                ],
                'sub' => $sub_details,
            ];
            $years += $y;
            $time_main = strtotime("+$y year", $time_main);

            if ($years_alive <= $years && $main_luck === null) $main_luck = $f;
        }
        $ret['main_luck'] = $main_luck;
        $ret['sub_luck'] = $sub_luck;
        $ret['years_alive'] = $years_alive_or;

        return $ret;
    }

    /**
     * 获取当前小限星 数组
     */
    private function getTheSubPlanets($main)
    {
        // 以当前大限主管星为起始星(小限)
        $sub_planets = config('astrolog.firdaria_sub');

        if ($main == 16 || $main == 12)
            return [$main];

        $temp = [];
        $start = false;
        $num = 0;
        foreach ($sub_planets as $s) {
            if ($s == $main || $start === true) {
                $temp[] = $s;
                $start = true;
                $num++;
                if ($num == 7) break;
            }
        }
        return $temp;
    }
}
